﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

namespace WebSite.admin.product
{
    public partial class goods_batch_select : System.Web.UI.Page
    {
    

        // Methods
        private void AddItemFromSourceListBox(ListBox sourceBox, ListBox targetBox, string aa)
        {
            string str = "0";
            foreach (ListItem item in targetBox.Items)
            {
                if (sourceBox.SelectedValue.ToString() == item.Value.ToString())
                {
                    str = "1";
                    break;
                }
            }
            if (aa.ToString() == "1")
            {
                foreach (ListItem item2 in sourceBox.Items)
                {
                    if ((item2.Selected && !targetBox.Items.Contains(item2)) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(item2.Text, "" + sourceBox.SelectedValue.ToString() + ""));
                    }
                }
            }
            else
            {
                foreach (ListItem item3 in sourceBox.Items)
                {
                    if (!targetBox.Items.Contains(item3) && (str.ToString() == "0"))
                    {
                        targetBox.Items.Add(new ListItem(item3.Text, "" + item3.Value + ""));
                    }
                }
            }
        }

        private void Bind()
        {
            string sql = "select * from DB_Pinpai where visible=1 and languages=" + info.languages("") + " order by pinpaiorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.propinpai.Items.Add(new ListItem(row["pinpainame"].ToString(), row["pinpaiid"].ToString()));
                }
            }
        }

        protected void btnToAllLeft_Click(object sender, EventArgs e)
        {
            this.RemoveSelectedItemAll(this.lbRight);
        }

        protected void btnToAllRight_Click(object sender, EventArgs e)
        {
            string aa = "";
            if (this.lbLeft.Items.Count > 0)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        protected void btnToLeft_Click(object sender, EventArgs e)
        {
            if (this.lbRight.SelectedItem != null)
            {
                this.RemoveSelectedItem(this.lbRight);
            }
        }

        protected void btnToRight_Click(object sender, EventArgs e)
        {
            string aa = "1";
            if (this.lbLeft.SelectedItem != null)
            {
                this.AddItemFromSourceListBox(this.lbLeft, this.lbRight, aa);
            }
        }

        private void Display(string parentid, string space)
        {
            string sql = "Select * From DB_Productclass Where parentid =" + parentid + " and languages=" + info.languages("") + " Order By classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.typeid.Items.Add(new ListItem(space + "" + row["classname"].ToString() + "", "" + row["classid"].ToString() + ""));
                    this.Display(row["classid"].ToString(), "|-" + space);
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
        }

        protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
        {
            string str = "0";
            if (this.Radio1.Checked)
            {
                for (int i = 0; i < this.lbRight.Items.Count; i++)
                {
                    str = str + "," + this.lbRight.Items[i].Value;
                }
            }
            else if (this.Radio2.Checked)
            {
                string sql = "Select * From DB_shoppro Where procode in ('" + this.turn(re.replace(this.cardcodes.Text)) + "')";
                DataTable table = new DataTable();
                table = SQLAccess.GreatDs(sql).Tables[0];
                if (table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        str = str + "," + row["proid"].ToString();
                    }
                }
            }
            if (this.Radio3.Checked)
            {
                base.Response.Write("<script language='javascript'>window.location.href='goods_batch_select_add.aspx?pid=" + str + "';</script>");
                base.Response.End();
            }
            else if (this.Radio4.Checked)
            {
                base.Response.Write("<script language='javascript'>window.location.href='goods_batch_select_add02.aspx?pid=" + str + "';</script>");
                base.Response.End();
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            string str = "0";
            string sql = "select * from DB_Shoppro where visible=1 and languages=" + info.languages("") + " ";
            if (this.typeid.SelectedValue.ToString() != "0")
            {
                sql = sql + " and typeid=" + this.typeid.SelectedValue.ToString() + "";
            }
            if (this.propinpai.SelectedValue.ToString() != "0")
            {
                sql = sql + " and propinpai=" + this.propinpai.SelectedValue.ToString() + "";
            }
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    for (int i = 0; i < this.lbLeft.Items.Count; i++)
                    {
                        if (row["proid"].ToString() == this.lbLeft.Items[i].Value.ToString())
                        {
                            str = "1";
                            break;
                        }
                    }
                    if (str.ToString() == "0")
                    {
                        this.lbLeft.Items.Add(new ListItem(row["proname"].ToString(), row["proid"].ToString()));
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            if (!base.IsPostBack)
            {
                this.Bind();
                this.Display("0", "|--");
            }
        }

        private void RemoveSelectedItem(ListBox listControl)
        {
            while (listControl.SelectedIndex != -1)
            {
                listControl.Items.RemoveAt(listControl.SelectedIndex);
            }
        }

        private void RemoveSelectedItemAll(ListBox listControl)
        {
            if (listControl.Items.Count > 0)
            {
                listControl.Items.Clear();
            }
        }

        public string turn(string str)
        {
            str = str.Replace("\n", ",");
            return str;
        }

      
    }
}